package com.fanling.venus.web.controller.system;

import com.fanling.venus.common.annotation.Log;
import com.fanling.venus.common.base.AjaxResult;
import com.fanling.venus.common.enums.BusinessType;
import com.fanling.venus.frame.util.ShiroUtils;
import com.fanling.venus.frame.web.BaseController;
import com.fanling.venus.frame.web.TableDataInfo;
import com.fanling.venus.system.domain.SysRole;
import com.fanling.venus.system.service.SysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色信息
 */
@Controller
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {
    private String prefix = "system/role";

    @Autowired
    private SysRoleService roleService;

    @RequiresPermissions("system:role:view")
    @GetMapping()
    public String role() {
        return prefix + "/role";
    }

    @RequiresPermissions("system:role:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysRole role) {
        startPage();
        List<SysRole> list = roleService.selectRoleList(role);
        return getDataTable(list);
    }

    /**
     * 新增角色
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/role-add";
    }

    /**
     * 新增保存角色
     */
    @RequiresPermissions("system:role:add")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult addSave(SysRole role) {
        AjaxResult validate = validate(role);
        if (validate == null) {
            role.setCreateBy(ShiroUtils.getLoginName());
            ShiroUtils.clearCachedAuthorizationInfo();
            return toAjax(roleService.insertRole(role));
        } else {
            return validate;
        }
    }

    /**
     * 验证
     *
     * @param role
     * @return
     */
    public AjaxResult validate(SysRole role) {
        if ("1".equals(roleService.checkRoleNameUnique(role))) {
            return error("角色名称已经存在！");
        }
        if ("1".equals(roleService.checkRoleKeyUnique(role))) {
            return error("角色权限已经存在！");
        }
        return null;
    }

    /**
     * 修改角色
     */
    @GetMapping("/edit/{roleId}")
    public String edit(@PathVariable("roleId") Long roleId, ModelMap mmap) {
        mmap.put("role", roleService.selectRoleById(roleId));
        return prefix + "/role-edit";
    }

    /**
     * 修改保存角色
     */
    @RequiresPermissions("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult editSave(SysRole role) {
        AjaxResult validate = validate(role);
        if (validate == null) {
            role.setUpdateBy(ShiroUtils.getLoginName());
            ShiroUtils.clearCachedAuthorizationInfo();
            return toAjax(roleService.updateRole(role));
        } else {
            return validate;
        }
    }

    /**
     * 新增数据权限
     */
    @GetMapping("/rule/{roleId}")
    public String rule(@PathVariable("roleId") Long roleId, ModelMap mmap) {
        mmap.put("role", roleService.selectRoleById(roleId));
        return prefix + "/rule";
    }

    /**
     * 修改保存数据权限
     */
    @RequiresPermissions("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PostMapping("/rule")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult ruleSave(SysRole role) {
        role.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(roleService.updateRule(role));
    }

    @RequiresPermissions("system:role:remove")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{roleId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("roleId") Long roleId) {
        if (roleService.deleteRoleById(roleId)) {
            return success("删除成功！");
        } else {
            return error("删除失败！");
        }
    }

    /**
     * 选择菜单树
     */
    @GetMapping("/selectMenuTree")
    public String selectMenuTree() {
        return prefix + "/tree";
    }
}